<?php
class Structure
{
    
    private $tableMenu;
    
    public function __construct()
    {
        $this->tableMenu = new Zend_Db_Table('menu');
    }
    

    public function getChildren($ID)
    {
         $Rows = $this->tableMenu->fetchAll( $this->tableMenu->select()->where('PID = ?', $ID)->order('weight'));
         if ($Rows)
         {
            return $Rows->toArray();
         }  
         else
         {
            return array();
         }
    }
    
    
    public function get($PMID)
    {
        $Row = $this->tableMenu->find($PMID)->current();
        if($Row) return $Row->toArray();
        
        return false;
    }
    
    
    public function add($PMID,$Name)
    {
        $Row = $this->tableMenu->createRow(array('PID'=>$PMID,'Name'=>$Name));
        $Row->save();
        return  $Row->ID;
    }
    
    public function updatelink($PMID,$Link)
    {
        $Row = $this->tableMenu->find($PMID)->current();
        $Row->Link = $Link;
        $Row->save();
    }
    
    public function delete($PMID)
    {
        $Row = $this->tableMenu->find($PMID)->current();
        if(!$Row) return;
        $Row->delete();
    }
     
    public function rename($PMID, $Name)
    {
        $Row = $this->tableMenu->find($PMID)->current();
        if($Row)
        {
            $Row->Name = $Name;
            $Row->save();
        }
    }
    
    
    public function move($PMID, $newParentId)
    {
        $Row = $this->tableMenu->find($PMID)->current();
        if($Row)
        {
            $Row->PID = $newParentId;
            $Row->save();
        }
    }
    
    
    public function setPosition($PMID, $newPosition)
    {
        
        $newPosition;
        $Row = $this->tableMenu->find($PMID)->current();
        $currentPosition = $Row->weight;

        Logger::log("PID: " . $PMID);
        Logger::log("Old position: " . $currentPosition);
        Logger::log("New position: " . $newPosition);
        
        echo $this->tableMenu->update(array('weight' => new Zend_Db_Expr('weight+1')), "weight >= $newPosition AND weight < $currentPosition");
        // V  >= currentPos and < newPos   weight++
//        perform replace     
        
            $Row->weight = $newPosition;
            $Row->save();
            
            
//            exit();
    }
    
     
    public function buildHierarchy()
    {
        $Levels = $this->getChildren(0);
        $Levels = $this->rBuild($Levels);
        return $Levels; 
    } 
    
    
    private function rBuild(&$Array)
    {
        foreach ($Array as $Key => $Item)
        {
            $Children = $this->getChildren($Item['ID']);
            $Array[$Key]['sub'] = $Children;
            $this->rBuild($Array[$Key]['sub']);
        }
        return $Array;
    } 
    
    
    public function buildJsTree($OrigTree)
    {
        
        $newJsTree = array();
        
        foreach ($OrigTree as $Item)
        {
        $jsItem = array('data' => $Item['Name'], 'attr'=>array('id'=>$Item['ID']));
        if (count($Item['sub'])) $jsItem['children'] = $this->buildJsTree($Item['sub']);
            $newJsTree[] = $jsItem;     
        }
        return $newJsTree;
    }
    
    
 
    public function ManiMenu()
    {
        $Category = new Structure();
        $MainCategories = $Category->getChildren(0); 
        $Layout = Zend_Layout::getMvcInstance();
        $view = $Layout->getView();
        $Front = Zend_Controller_Front::getInstance();
        return $view->partial( '/menu/main.phtml',array('MC'=>$MainCategories));
    } 
    
    
    public function buildSelect($Level = 0)
    {
        $Hierarchy =$this->buildHierarchy();
        $Layout = Zend_Layout::getMvcInstance();
        $view = $Layout->getView();
        
        $Front = Zend_Controller_Front::getInstance();
        
        if ($Level==0)
        return $view->partial('/partial/menu/select.phtml',array('Elements'=>$Hierarchy));
        else
        return $view->partial('/partial/menu/selectone.phtml',array('Elements'=>$Hierarchy));
    }

    public function buildULLI()
    {
        $Hierarchy =$this->buildHierarchy();
        $Layout = Zend_Layout::getMvcInstance();
        $view = $Layout->getView();
        
        $Front = Zend_Controller_Front::getInstance();
//        $Lang = $Front->getRequest()->getParam('lang');
        
        return $view->partial('/partial/menu/ulli.phtml',array('Elements'=>$Hierarchy));
    } 
    
    
}